<?php

declare(strict_types=1);

namespace App\CustHub\Controller;

use App\CustHub\Annotation\ApiAuth;
use App\CustHub\Annotation\VerifySignature;
use App\CustHub\Dto\RoomDto;
use App\CustHub\Request\RoomRequest;
use App\CustHub\Service\ChRoomBehaviorLogService;
use App\CustHub\Service\ChRoomService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;

/**
 * 群管理
 */
#[Controller('ch/room'),ApiAuth,VerifySignature]
class RoomController extends BaseController
{
    public array $noNeedLogin = ['exportRoomMember','export'];
    public array $noNeedVerifySign = ['exportRoomMember','export'];

    #[Inject]
    protected ChRoomService $service;

    #[Inject]
    protected ChRoomBehaviorLogService $behaviorLogService;

    /**
     * 群列表
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('list')]
    public function list(): ResponseInterface
    {
        return $this->success($this->service->list($this->request->all()));
    }

    #[GetMapping('export')]
    public function export(): ResponseInterface
    {
        return $this->service->export($this->request->all(),RoomDto::class);
    }

    /**
     * 群选择列表
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('selectList')]
    public function selectList(): ResponseInterface
    {
        return $this->success($this->service->selectPageList($this->request->all()));
    }

    /**
     * 详情
     * @param RoomRequest $request
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('detail')]
    public function detail(RoomRequest $request): ResponseInterface
    {
        return $this->success($this->service->roomDetail($request->all()));
    }

    /**
     * 群成员列表
     * @param RoomRequest $request
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('getRoomMemberList')]
    public function getRoomMemberList(): ResponseInterface
    {
        return $this->success($this->service->getRoomMemberList($this->request->all()));
    }

    /**
     * 群邀请列表
     * @param RoomRequest $request
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('inviteList')]
    public function inviteList(RoomRequest $request): ResponseInterface
    {
        return $this->success($this->service->inviteList($request->all()));
    }

    /**
     * 群邀请详情列表
     * @param RoomRequest $request
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('inviteDetail')]
    public function inviteDetail(RoomRequest $request): ResponseInterface
    {
        return $this->success($this->service->inviteDetails($request->all()));
    }

    /**
     * 导出群成员数据
     * @param RoomRequest $request
     * @return ResponseInterface
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[GetMapping('exportRoomMember')]
    public function exportRoomMember(RoomRequest $request): ResponseInterface
    {
        return $this->service->exportRoomMember($request->all());
    }

    /**
     * 群同步
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('syncRoom')]
    public function syncRoom(RoomRequest $request): ResponseInterface
    {
        return  $this->service->batchSyncRoom($request->input('robot_ids')) ? $this->success("群同步中，请前往同步群任务中查看进度") : $this->error('群同步中~');
    }

    /**
     * 同步群成员
     * @param RoomRequest $request
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('syncRoomMember')]
    public function syncRoomMember(RoomRequest $request): ResponseInterface
    {
        return $this->service->syncRoomMember($request->all()) ? $this->success('群成员同步中，请稍后刷新本页面查看') : $this->error('刷新失败') ;
    }

    /**
     * 群行为日志
     * @param RoomRequest $request
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('behaviorLog')]
    public function behaviorLog(RoomRequest $request): ResponseInterface
    {
        return $this->success($this->behaviorLogService->getLog($request->all()));
    }

    /**
     * 获取群内机器人列表
     * @param RoomRequest $request
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('getRoomRobotList')]
    public function getRoomRobotList(RoomRequest $request): ResponseInterface
    {
        return $this->success($this->service->getRoomRobotList($request->all()));
    }

    #[PostMapping('getRobotPageList')]
    public function getRobotPageList(RoomRequest $request): ResponseInterface
    {
        return $this->success($this->service->getRobotPageList($request->all()));
    }

    /**
     * 获取同企业成员列表
     * @param RoomRequest $request
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('fetchCorpRoomMembers')]
    public function fetchCorpRoomMembers(RoomRequest $request): ResponseInterface
    {
        return $this->success($this->service->fetchCorpRoomMembers($request->all()));
    }

    /**
     * 获取可转让的群列表
     * @param RoomRequest $request
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('getTransferableRooms')]
    public function getTransferableRooms(RoomRequest $request): ResponseInterface
    {
        return $this->success($this->service->getTransferableRooms($request->all()));
    }

    /**
     * 获取可设置管理员的群列表
     * @param RoomRequest $request
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('getAdminAssignableRooms')]
    public function getAdminAssignableRooms(RoomRequest $request): ResponseInterface
    {
        return $this->success($this->service->getAdminAssignableRooms($request->all()));
    }

    /**
     * 群主转让
     * @param RoomRequest $request
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     * @throws \RedisException
     */
    #[PostMapping('changeRoomOwner')]
    public function changeRoomOwner(RoomRequest $request): ResponseInterface
    {
        $this->service->changeRoomOwner($request->all());
        return $this->success("操作任务提交成功，可在 “任务中心” 查看进度");
    }

    /**
     * 设置管理员
     * @param RoomRequest $request
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     * @throws \RedisException
     */
    #[PostMapping('setRoomAdmin')]
    public function setRoomAdmin(RoomRequest $request): ResponseInterface
    {
        $this->service->setRoomAdmin($request->all());
        return $this->success("操作任务提交成功，可在 “任务中心” 查看进度");
    }

    /**
     * 修改群名称
     * @param RoomRequest $request
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('editRoomName')]
    public function editRoomName(RoomRequest $request): ResponseInterface
    {
        $this->service->editRoomName($request->all());
        return $this->success("操作任务提交成功，可在 “任务中心” 查看进度");
    }

    /**
     * 修改群名称
     * @param RoomRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('renameRoom')]
    public function renameRoom(RoomRequest $request): ResponseInterface
    {
        $this->service->renameRoom($request->all());
        return $this->success("修改成功");
    }

    /**
     * 退群
     * @param RoomRequest $request
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('outRoom')]
    public function outRoom(RoomRequest $request): ResponseInterface
    {
        return $this->service->outRoom($request->all()) ? $this->success('操作成功') : $this->error('操作失败');
    }

    /**
     * 批量退群
     * @param RoomRequest $request
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('batchOutRoom')]
    public function batchOutRoom(RoomRequest $request): ResponseInterface
    {
        return $this->service->batchOutRoom($request->all()) ? $this->success('操作批量退群提交成功，请稍后刷新当前页面查看') : $this->error('操作失败');
    }

    /**
     * 保存通讯录
     * @param RoomRequest $request
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('saveContacts')]
    public function saveContacts(RoomRequest $request): ResponseInterface
    {
        return $this->service->saveContacts($request->all()) ? $this->success('操作任务提交成功，可在 “任务中心” 查看进度') : $this->error('操作失败');
    }

    /**
     * 移除通讯录
     * @param RoomRequest $request
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('removeFromContacts')]
    public function removeFromContacts(RoomRequest $request): ResponseInterface
    {
        return $this->service->removeFromContacts($request->all()) ? $this->success('操作任务提交成功，可在 “任务中心” 查看进度') : $this->error('操作失败');
    }


    /**
     * 批量删除群
     * @param RoomRequest $request
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('batchDelRoom')]
    public function batchDelRoom(RoomRequest $request): ResponseInterface
    {
        return $this->service->batchDelRoom($request->all()) ? $this->success('删除成功') : $this->error('删除失败');
    }

    #[PostMapping('getQrCode')]
    public function getQrCode(RoomRequest $request): ResponseInterface
    {
        return $this->success(['qr_code'=>$this->service->getQrCode($request->all())]);
    }

    /**
     * 获取群主和管理员列表
     * @param RoomRequest $request
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('getOwnerAndAdminRobotList')]
    public function getOwnerAndAdminRobotList(RoomRequest $request)
    {
        return $this->success($this->service->getOwnerAndAdminRobotList($request->all()));
    }
    /**
     * 设置客户群免打扰
     * @param RoomRequest $request
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('setRoomDoNotDisturb')]
    public function setRoomDoNotDisturb(RoomRequest $request): ResponseInterface
    {
        return $this->service->setRoomDoNotDisturb($request->all()) ? $this->success('操作任务提交成功，可在 “任务中心” 查看进度') : $this->error('操作失败');
    }

    /**
     * @param RoomRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('getRobotList')]
    public function getRobotList(RoomRequest $request): ResponseInterface
    {
        return $this->success($this->service->getRobotList($request->all()));
    }

    /**
     * 发送群公告
     * @param RoomRequest $request
     * @return mixed
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('publishRoomNotice')]
    public function publishRoomNotice(RoomRequest $request): mixed
    {
        return $this->service->publishRoomNotice($request->all()) ? $this->success('发布成功') : $this->error('发布失败');
    }

    #[PostMapping('getPushMsgList')]
    public function getPushMsgList(RoomRequest $request): ResponseInterface
    {
        return $this->success($this->service->getPushMsgList($request->all()));
    }

    /**
     * 群消息推送
     * @param RoomRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('pushRoomMsg')]
    public function pushRoomMsg(RoomRequest $request): ResponseInterface
    {
        return $this->service->pushRoomMsg($request->all()) ? $this->success('操作成功') : $this->error('操作失败');
    }
}